package LOGIN;

import entity.Admin;
import mapper.impl.AdmMapperImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/zc")
public class registration extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Admin admin = new Admin();
        String t_No = req.getParameter("t_No");
        String name = req.getParameter("name");
        String account = req.getParameter("account");
        String password = req.getParameter("password");


//        判断注册页面是否为空
        if ((t_No == null || name == null || account == null || password == null) || (t_No == "" || name == "" || account == "" || password == "")) {
            req.setAttribute("errorMessage", "用户或密码不能为空");
            req.getRequestDispatcher("/error(null).jsp").forward(req, resp);
            return;
        }

//        数据存储
        admin.setT_No(t_No);
        admin.setName(name);
        admin.setAccount(account);
        admin.setPassword(password);
        AdmMapperImpl admMapper = new AdmMapperImpl();
        boolean insert = admMapper.add(t_No, name, account, password);
        if (insert == false) {
            req.setAttribute("errorMessage", "账号已经存在,请重新输入账号");
            req.getRequestDispatcher("error(null).jsp").forward(req, resp);
        } else {
            //设置作用域的值
            req.setAttribute("errorMessage", "你注册成功");
            //跳转页面【别tm用中文】 errorMessage
            //你设置的名字和取的名字是一样的吗？  你为什么要改后端呢？你后端有很多地方都是errormessage，而你前端只有一个地方是errorMessage，肯定改少的地方
            req.getRequestDispatcher("/error(null).jsp").forward(req, resp);
        }
    }
}

